<template>
  <div id="wrap">
    <van-button type="default" @click="getPrinterStatus()"
      >查询打印机状态</van-button
    >
    <van-button type="primary" @click="getConnectState()"
      >查询打印机连接状态</van-button
    >
    <van-button type="primary" @click="connectBleByMacAddress()"
      >连接打印机（mac）</van-button
    >
    <van-button type="primary" @click="connectUsb()"
      >连接打印机（USB）</van-button
    >
    <van-button type="info" @click="printTest()">打印测试页</van-button>
    <van-button type="danger" @click="printLabel()"
      >打印标签(TSC模式打印)</van-button
    >
    <van-button type="danger" @click="disConnect()">断开连接</van-button>
  </div>
</template>
<script>
export default {
  name: "test",
  data() {
    return {
      demo: null,
    };
  },
  mounted() {
    const that = this;

    if (process.env.NODE_ENV === "production") {
      that.$nextTick(() => {
        that.demo = window.api.require("xGprinterPlus");
      });
    } else {
      window.apiready = () => {
        that.demo = window.api.require("xGprinterPlus");
      };
    }
  },
  methods: {
    // 根据mac地址连接蓝牙设备
    connectBleByMacAddress() {
      this.demo.connectBleByMacAddress(
        {
          macAddress: "00:15:83:36:AB:EC",
        },
        function (ret, err) {
          alert(JSON.stringify(ret) + "   " + JSON.stringify(err));
        }
      );
    },
    // USB
    connectUsb() {
      this.demo.connectUsb(function (ret, err) {
        alert(JSON.stringify(ret) + "   " + JSON.stringify(err));
      });
    },
    // 打印测试页
    printTest() {
      this.demo.printTest(function (ret, err) {
        alert(JSON.stringify(ret) + "   " + JSON.stringify(err));
      });
    },
    // 查询打印机状态
    getPrinterStatus() {
      this.demo.getPrinterStatus(function (ret, err) {
        alert(JSON.stringify(ret) + "   " + JSON.stringify(err));
      });
    },
    //  查询打印机状态
    getConnectState() {
      var obj = window.api.require("xGprinterPlus");
      var ret = obj.getConnectState();
      alert(JSON.stringify(ret));
    },
    // 打印标签
    printLabel() {
      this.demo.printLabel(
        {
          rows: [
            {
              lineType: "size",
              width: 80,
              height: 72,
            },
            {
              lineType: "gap",
              data: 0,
            },
            {
              lineType: "cls",
            },
            {
              lineType: "gap",
              data: 0,
            },
            {
              // 左边框
              lineType: "bar",
              x: 16,
              y: 0,
              width: 4,
              height: 556,
            },
            {
              // 上边框
              lineType: "bar",
              x: 16,
              y: 0,
              width: 608,
              height: 8,
            },
            {
              // 右边框
              lineType: "bar",
              x: 624,
              y: 0,
              width: 4,
              height: 556,
            },
            {
              // 下边框
              lineType: "bar",
              x: 16,
              y: 556,
              width: 608,
              height: 4,
            },
            {
              // 图片单元格竖线
              lineType: "bar",
              x: 16 + 130,
              y: 0,
              width: 2,
              height: 106,
            },
            {
              //绘制图片
              lineType: "image",
              x: 16 + 8,
              y: 8,
              data: "widget://image/huibo_logo.png",
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 8,
              y: 8,
              rotation: 0,
              xscal: 1,
              yscal: 1,
              data: "青岛汇博通商贸有限公司",
            },
            {
              // 图片单元格横线
              lineType: "bar",
              x: 16 + 130,
              y: 56,
              width: 640 - 130 - 16 - 16,
              height: 2,
            },
            {
              // 二维码旁边竖线
              lineType: "bar",
              x: 16 + 320,
              y: 106,
              width: 2,
              height: 556 - 106,
            },
            {
              // 横线
              lineType: "bar",
              x: 16,
              y: 106,
              width: 608,
              height: 2,
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 4,
              y: 106 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "捆包号：R103571234",
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 320 + 2,
              y: 106 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "钢厂资源号：ML123456789",
            },
            {
              // 横线
              lineType: "bar",
              x: 16 + 320 + 2,
              y: 156,
              width: 608,
              height: 2,
            },
            {
              //绘制code128
              lineType: "code128",
              x: 16 + 16,
              y: 156 + 2,
              height: 48,
              rotation: 0,
              data: "1234567890",
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 320 + 2,
              y: 156 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "母卷号：P123456789",
            },
            {
              // 横线
              lineType: "bar",
              x: 16,
              y: 206,
              width: 608,
              height: 2,
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 4,
              y: 206 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "品名：冷轧卷",
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 320 + 2,
              y: 206 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "牌号：DC06",
            },
            {
              // 横线
              lineType: "bar",
              x: 16,
              y: 256,
              width: 608,
              height: 2,
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 4,
              y: 256 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "规格：1.2*1250*C",
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 320 + 2,
              y: 256 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "净重：10.5t",
            },
            {
              // 横线
              lineType: "bar",
              x: 16,
              y: 306,
              width: 608,
              height: 2,
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 4,
              y: 306 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "毛重：10.8t",
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 320 + 2,
              y: 306 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "件数：1",
            },
            {
              // 横线
              lineType: "bar",
              x: 16,
              y: 356,
              width: 608,
              height: 2,
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 4,
              y: 356 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "库区：原料区",
            },
            {
              //绘制qrcode
              lineType: "qrcode",
              x: 16 + 320 + 2 + 100,
              y: 356 + 2 + 25,
              level: "L",
              cellwith: 5,
              rotation: 0,
              data: "捆包号|钢厂资源号|母卷号|品名|牌号|规格|净重|毛重",
            },
            {
              // 横线
              lineType: "bar",
              x: 16,
              y: 406,
              width: 320,
              height: 2,
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 4,
              y: 406 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "库位：YC-02",
            },
            {
              // 横线
              lineType: "bar",
              x: 16,
              y: 456,
              width: 320,
              height: 2,
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 4,
              y: 456 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "收货单位:英大",
            },
            {
              // 横线
              lineType: "bar",
              x: 16,
              y: 506,
              width: 320,
              height: 2,
            },
            {
              //绘制文本
              lineType: "text",
              x: 16 + 4,
              y: 506 + 2,
              rotation: 0,
              xscal: 1,
              yscal: 2,
              data: "日期:2016.06.12",
            },
          ],
        },
        function (ret, err) {
          alert(JSON.stringify(ret) + "   " + JSON.stringify(err));
        }
      );
    },
    // 断开连接
    disConnect() {
      this.demo.disConnect(function (ret, err) {
        alert(JSON.stringify(ret) + "   " + JSON.stringify(err));
      });
    },
  },
};
</script>